package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TbGoods;
import com.ruoyi.system.service.ITbGoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品设置Controller
 *
 * @author ruoyi
 * @date 2021-02-19
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/goods" )
public class TbGoodsController extends BaseController {

    private final ITbGoodsService iTbGoodsService;

    /**
     * 查询商品设置列表
     */
    @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbGoods tbGoods) {
        startPage();
        List<TbGoods> list = iTbGoodsService.queryList(tbGoods);
        return getDataTable(list);
    }


    /**
     * 查询商品设置列表
     */
    @GetMapping("/lists")
    public TableDataInfo lists(TbGoods tbGoods) {

        List<TbGoods> list = iTbGoodsService.list(null);
        ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();
        for (TbGoods goods : list) {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("id",goods.getId());
            hashMap.put("img",goods.getImages());
            hashMap.put("title",goods.getGoodsTitle());
            hashMap.put("price",String.valueOf(goods.getMoney()));
            arrayList.add(hashMap);
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");

        rspData.setRows(arrayList);
        return rspData;
    }

    /**
     * 导出商品设置列表
     */
    @PreAuthorize("@ss.hasPermi('system:goods:export')" )
    @Log(title = "商品设置" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(TbGoods tbGoods) {
        List<TbGoods> list = iTbGoodsService.queryList(tbGoods);
        ExcelUtil<TbGoods> util = new ExcelUtil<TbGoods>(TbGoods.class);
        return util.exportExcel(list, "goods" );
    }

    /**
     * 获取商品设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:goods:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(iTbGoodsService.getById(id));
    }

    /**
     * 新增商品设置
     */
    @PreAuthorize("@ss.hasPermi('system:goods:add')" )
    @Log(title = "商品设置" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbGoods tbGoods) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        tbGoods.setUserId(userId + "");
        return toAjax(iTbGoodsService.save(tbGoods) ? 1 : 0);
    }

    /**
     * 修改商品设置
     */
    @PreAuthorize("@ss.hasPermi('system:goods:edit')" )
    @Log(title = "商品设置" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbGoods tbGoods) {
        System.out.println(SecurityUtils.getLoginUser());
        return toAjax(iTbGoodsService.updateById(tbGoods) ? 1 : 0);
    }

    /**
     * 删除商品设置
     */
    @PreAuthorize("@ss.hasPermi('system:goods:remove')" )
    @Log(title = "商品设置" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(iTbGoodsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
